#region License

// This file is part of Grawlix.
// 
// Grawlix is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
// Grawlix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public
// License along with Grawlix.  If not, see
// <http://www.gnu.org/licenses/>.

#endregion

using System;
using System.Diagnostics;
using System.IO;

namespace Grawlix.Services.Resources
{
	/// <summary>
	/// A convenience implementation of typed resolver functions.
	/// </summary>
	/// <typeparam name="T">The type of resource that is resolved.
	/// </typeparam>
	public abstract class TypedResolverBase<T> :
		ServiceBase<ITypedResolver<T>>, ITypedResolver<T>
		where T : class
	{
		/// <summary>
		/// The resolver which accesses resources.
		/// </summary>
		private readonly IUriResolver _uriResolver;

		/// <summary>
		/// Initializes a new instance of the
		/// <see cref="TypedResolverBase&lt;T&gt;"/> class.
		/// </summary>
		/// <param name="resolver">The resolver which accesses
		/// resources.</param>
		protected TypedResolverBase(IUriResolver resolver)
		{
			_uriResolver = resolver;
		}

		/// <summary>
		/// The resolver which accesses resources.
		/// </summary>
		protected IUriResolver UriResolver
		{
			get { return _uriResolver; }
		}

		/// <summary>
		/// Converts a resource object into the appropriate form.
		/// </summary>
		/// <param name="uri">The uri that identifies the typed
		/// resource.</param>
		/// <param name="resource">The untyped resource data.</param>
		/// <returns>The typed resource.</returns>
		/// <exception cref="IOException">A generic I/O error occurred.
		/// </exception>
		/// <exception cref="FileNotFoundException">The resource is
		/// the wrong type.
		/// </exception>
		/// <remarks>This is a blocking operation which should only be
		/// called asynchronously.</remarks>
		public virtual T Convert(Uri uri, object resource)
		{
			var directResource = resource as T;
			if (directResource != null)
				return directResource;
			var streamResource = resource as Stream;
			if (streamResource != null)
				return Parse(streamResource);
			throw new FileNotFoundException(
				"The resource specified by URI (" + uri + ") " +
				"was not in a form convertible to " + typeof(T) + ".");
		}

		/// <summary>
		/// Loads a typed resource by its URI.
		/// </summary>
		/// <param name="uri">The uri that identifies the typed
		/// resource.</param>
		/// <returns>The typed resource found.</returns>
		/// <exception cref="IOException">A generic I/O error occurred.
		/// </exception>
		/// <exception cref="FileNotFoundException">The URI could not
		/// be resolved or is of the wrong type.
		/// </exception>
		/// <remarks>This is a blocking operation which should only be
		/// called asynchronously.</remarks>
		public virtual T Load(Uri uri)
		{
			object resource = UriResolver.Load(uri);
			Debug.Assert(resource != null);
			return Convert(uri, resource);
		}

		/// <summary>
		/// Parses the specified resource stream into the appropriate
		/// type.
		/// </summary>
		/// <param name="resourceStream">The resource stream to parse.
		/// </param>
		/// <returns>The created object.</returns>
		protected abstract T Parse(Stream resourceStream);
	}
}